package com.ruoyi.tzai.domain;

import java.math.BigDecimal;
import java.util.Date;
import com.fasterxml.jackson.annotation.JsonFormat;
import com.ruoyi.common.annotation.Excel;
import com.ruoyi.common.core.domain.BaseEntity;
import org.apache.commons.lang3.builder.ToStringBuilder;
import org.apache.commons.lang3.builder.ToStringStyle;

/**
 * API Key池对象 tzai_api_key_pool
 * 
 * @author tzai
 * @date 2025-11-12
 */
public class ApiKeyPool extends BaseEntity
{
    private static final long serialVersionUID = 1L;

    /** Key ID */
    private Long keyId;

    /** 资源类型 */
    @Excel(name = "资源类型", readConverterExp = "ai_model=AI模型,tts_engine=TTS引擎")
    private String resourceType;

    /** 资源ID */
    @Excel(name = "资源ID")
    private Long resourceId;

    /** API Key */
    private String apiKey;

    /** API Secret */
    private String apiSecret;

    /** Key名称/备注 */
    @Excel(name = "Key名称")
    private String keyName;

    /** 状态 */
    @Excel(name = "状态", readConverterExp = "active=激活,disabled=禁用,rate_limited=限流中,expired=已过期")
    private String status;

    /** 优先级 */
    @Excel(name = "优先级")
    private Integer priority;

    /** 每分钟速率限制 */
    @Excel(name = "每分钟限制")
    private Integer rateLimitPerMinute;

    /** 最大并发数 */
    @Excel(name = "最大并发数")
    private Integer maxConcurrency;

    /** 当前并发数 */
    @Excel(name = "当前并发")
    private Integer currentConcurrency;

    /** 总调用次数 */
    @Excel(name = "总调用次数")
    private Long totalCalls;

    /** 成功次数 */
    @Excel(name = "成功次数")
    private Long successCalls;

    /** 失败次数 */
    @Excel(name = "失败次数")
    private Long failedCalls;

    /** 成功率(%) */
    @Excel(name = "成功率")
    private BigDecimal successRate;

    /** 平均响应时间(秒) */
    @Excel(name = "平均响应时间")
    private BigDecimal avgResponseTime;

    /** 最后调用时间 */
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    @Excel(name = "最后调用时间", width = 30, dateFormat = "yyyy-MM-dd HH:mm:ss")
    private Date lastCallTime;

    /** 最后错误信息 */
    private String lastError;

    /** 限流恢复时间 */
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    private Date rateLimitResetTime;

    public void setKeyId(Long keyId) 
    {
        this.keyId = keyId;
    }

    public Long getKeyId() 
    {
        return keyId;
    }

    public void setResourceType(String resourceType) 
    {
        this.resourceType = resourceType;
    }

    public String getResourceType() 
    {
        return resourceType;
    }

    public void setResourceId(Long resourceId) 
    {
        this.resourceId = resourceId;
    }

    public Long getResourceId() 
    {
        return resourceId;
    }

    public void setApiKey(String apiKey) 
    {
        this.apiKey = apiKey;
    }

    public String getApiKey() 
    {
        return apiKey;
    }

    public void setApiSecret(String apiSecret) 
    {
        this.apiSecret = apiSecret;
    }

    public String getApiSecret() 
    {
        return apiSecret;
    }

    public void setKeyName(String keyName) 
    {
        this.keyName = keyName;
    }

    public String getKeyName() 
    {
        return keyName;
    }

    public void setStatus(String status) 
    {
        this.status = status;
    }

    public String getStatus() 
    {
        return status;
    }

    public void setPriority(Integer priority) 
    {
        this.priority = priority;
    }

    public Integer getPriority() 
    {
        return priority;
    }

    public void setRateLimitPerMinute(Integer rateLimitPerMinute) 
    {
        this.rateLimitPerMinute = rateLimitPerMinute;
    }

    public Integer getRateLimitPerMinute() 
    {
        return rateLimitPerMinute;
    }

    public void setMaxConcurrency(Integer maxConcurrency) 
    {
        this.maxConcurrency = maxConcurrency;
    }

    public Integer getMaxConcurrency() 
    {
        return maxConcurrency;
    }

    public void setCurrentConcurrency(Integer currentConcurrency) 
    {
        this.currentConcurrency = currentConcurrency;
    }

    public Integer getCurrentConcurrency() 
    {
        return currentConcurrency;
    }

    public void setTotalCalls(Long totalCalls) 
    {
        this.totalCalls = totalCalls;
    }

    public Long getTotalCalls() 
    {
        return totalCalls;
    }

    public void setSuccessCalls(Long successCalls) 
    {
        this.successCalls = successCalls;
    }

    public Long getSuccessCalls() 
    {
        return successCalls;
    }

    public void setFailedCalls(Long failedCalls) 
    {
        this.failedCalls = failedCalls;
    }

    public Long getFailedCalls() 
    {
        return failedCalls;
    }

    public void setSuccessRate(BigDecimal successRate) 
    {
        this.successRate = successRate;
    }

    public BigDecimal getSuccessRate() 
    {
        return successRate;
    }

    public void setAvgResponseTime(BigDecimal avgResponseTime) 
    {
        this.avgResponseTime = avgResponseTime;
    }

    public BigDecimal getAvgResponseTime() 
    {
        return avgResponseTime;
    }

    public void setLastCallTime(Date lastCallTime) 
    {
        this.lastCallTime = lastCallTime;
    }

    public Date getLastCallTime() 
    {
        return lastCallTime;
    }

    public void setLastError(String lastError) 
    {
        this.lastError = lastError;
    }

    public String getLastError() 
    {
        return lastError;
    }

    public void setRateLimitResetTime(Date rateLimitResetTime) 
    {
        this.rateLimitResetTime = rateLimitResetTime;
    }

    public Date getRateLimitResetTime() 
    {
        return rateLimitResetTime;
    }

    /**
     * 计算成功率（动态计算）
     */
    public Double calculateSuccessRate() {
        if (totalCalls == null || totalCalls == 0) {
            return 0.0;
        }
        if (successCalls == null) {
            return 0.0;
        }
        return (successCalls * 100.0) / totalCalls;
    }

    /**
     * 判断是否可用
     */
    public boolean isAvailable() {
        // 状态必须是available
        if (!"available".equals(status)) {
            return false;
        }
        
        // 如果正在限流中,检查是否已恢复
        if (rateLimitResetTime != null && new Date().before(rateLimitResetTime)) {
            return false;
        }
        
        // 并发数未达上限
        if (currentConcurrency != null && maxConcurrency != null && currentConcurrency >= maxConcurrency) {
            return false;
        }
        
        return true;
    }

    @Override
    public String toString() {
        return new ToStringBuilder(this,ToStringStyle.MULTI_LINE_STYLE)
            .append("keyId", getKeyId())
            .append("resourceType", getResourceType())
            .append("resourceId", getResourceId())
            .append("apiKey", getApiKey())
            .append("apiSecret", getApiSecret())
            .append("keyName", getKeyName())
            .append("status", getStatus())
            .append("priority", getPriority())
            .append("rateLimitPerMinute", getRateLimitPerMinute())
            .append("maxConcurrency", getMaxConcurrency())
            .append("currentConcurrency", getCurrentConcurrency())
            .append("totalCalls", getTotalCalls())
            .append("successCalls", getSuccessCalls())
            .append("failedCalls", getFailedCalls())
            .append("successRate", getSuccessRate())
            .append("avgResponseTime", getAvgResponseTime())
            .append("lastCallTime", getLastCallTime())
            .append("lastError", getLastError())
            .append("rateLimitResetTime", getRateLimitResetTime())
            .append("createBy", getCreateBy())
            .append("createTime", getCreateTime())
            .append("updateBy", getUpdateBy())
            .append("updateTime", getUpdateTime())
            .toString();
    }
}

